﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace RegexTester.Helpers
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class ValidateRegexAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ValidationResult result = ValidationResult.Success;
            try
            {
                string text = (string)value;
                Regex r = new Regex(text);
            }
            catch (Exception ex)
            {
                result = new ValidationResult(ex.Message.Substring(ex.Message.LastIndexOf("\" - ") + 4));
            }

            return result;
        }

        //public override bool IsValid(object value)
        //{
        //    bool result = true;
        //    try
        //    {
        //        string text = (string)value;
        //        Regex r = new Regex(text);
        //    }
        //    catch (Exception)
        //    {
        //        result = false;
        //    }
            

        //    return result;
        //}
    }
}
